package com.apps.ftapp.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.NoticeAdapter;
import com.apps.ftapp.model.NoticeModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

public class NoticeActivity extends BaseActivity {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private Gson gson = new Gson();

    private NoticeAdapter noticeAdapter;
    private List<NoticeModel.MessageBean> dataList = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);
        ButterKnife.bind(this);


        setTitle("我的消息");

        initView();

        loadData();


    }

    private void loadData() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        Async.post("article", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", ">>>>>" + new String(responseBody));
                    NoticeModel result = gson.fromJson(new String(responseBody), NoticeModel.class);
                    if (1 == Tools.isIntNull(result.getSuccess())) {
                        dataList.clear();
                        dataList.addAll(result.getMessage());

//                        NoticeModel.MessageBean msg = new NoticeModel.MessageBean();
//                        msg.setId("暂无消息");
//                        msg.setTime("2017-10-25");
//                        msg.setTitle("上课通知");
//                        dataList.add(msg);
//
//                        NoticeModel.MessageBean msg1 = new NoticeModel.MessageBean();
//                        msg1.setId("暂无消息");
//                        msg1.setTime("2017-10-25");
//                        msg1.setTitle("模考通知");
//                        dataList.add(msg1);
//
//                        result.getMessage().add(0, msg);
//                        result.getMessage().add(1, msg1);
                        noticeAdapter.addRes(result.getMessage());

                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                L.i("main", ">>>>>" + new String(responseBody));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
        loadData();
    }

    private void initView() {

        noticeAdapter = new NoticeAdapter(getApplicationContext(), dataList);
        listView.setAdapter(noticeAdapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                HashMap map = new HashMap();
                map.put("id", dataList.get(position).getId());
                ActivitySkipUtil.skipAnotherActivity(NoticeActivity.this, NoticeDetailActivity.class, map);


            }
        });

    }
}
